<?php
# +--------------------------------------------------------
# |Description: 河北新点电商接口
# +--------------------------------------------------------
# |Company: 北京申合信科技发展有限公司
# +--------------------------------------------------------
# |WebSite: www.shenhexin.com
# +--------------------------------------------------------
# |Author: Akon<18363851918@163.com>
# +--------------------------------------------------------
# |Date: 2017年6月23日
# +--------------------------------------------------------

namespace Ecservice\Controller;
use Think\Controller;
class CommonController extends Controller{
    /**
     * 校验access_token
     * @param $access_token access_token
     * @param $name appKey
     */
    function checkAccessToken($access_token, $name){
        $this->errorMsg($access_token,'error token!');        //校验access_token是否传递
        $user_info = M('members','sdb_','DB_CONFIG2')->where(array('access_token'=>$access_token, 'name'=>$name))
                                                     ->find();
        $this->errorMsg($user_info,'请检查输入appKey或accessToken是否有误！');          //校验access_token是否正确
        $expire    = $user_info['create_time'] + 86400;
        if($expire < time()){                                //校验access_token是否过期
            $res = array(
                'returnMsg' => 'token expired',
                'isSuccess'    => false
            );
            exit(json_encode($res, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
        }
    }

    /**
     * 校验参数是否合法
     * @param $param  传递过来的参数
     * @param $msg    错误信息
     * @param $flag   功能标志
     * @param $num    参数个数
     *
     * $flag = 0 : 校验参数是否为空，$param为具体的参数
     * $flag = 1 : 校验参数是否合法，多传或少传都视为不合法，$param为将接受收据转后的数组
     */
    function errorMsg($param,$msg,$flag=0,$num=0){
        $res = array(
            'returnMsg' => $msg,
            'isSuccess' => false
        );
        if($flag == 0) {
            if (!$param) {
                exit(json_encode($res, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
            }
        }else if($flag == 1){
            if(!is_array($param)){
                exit(json_encode($res, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
            }else{
                if(count($param) > $num) {
                    exit(json_encode($res, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
                }else if(count($param) < $num) {
                    exit(json_encode($res, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
                }
            }
        }
    }

    /**
     * 检查用户是否存在
     * @param $name     appKey
     * @param $msg      错误信息
     */
    function checkUser($name, $msg){
        $res = M('members', 'sdb_', 'DB_CONFIG2')->where(array('name'=>$name))
                                                 ->count();
        $this->errorMsg($res, $msg);
    }

    /**
     * 校验参数及accessToken（公用校验部分）
     * @param $appKey   appKey
     * @param $token    accessToken
     * @param $receive      $receive
     * @param $num      数组长度
     * @param $name     appKey
     */
    function checkParam($appKey, $token, $receive, $num){
        //校验参数
        $this->errorMsg($receive, '参数不合法', 1 , $num);
        $this->errorMsg($appKey, 'appKey不能为空');
        $this->errorMsg($token, 'accessToken不能为空');
        $this->checkUser($appKey, 'appKey输入有误');
        //2、校验accessToken
        $this->checkAccessToken($token, $appKey);
    }
}